<template>
  <div class="order-list-container">
    <h2>我的订单</h2>
    <div v-if="loading">加载中...</div>
    <div v-else>
      <div class="order-card-list">
        <div v-for="order in orders" :key="order.id" class="order-card">
          <img :src="order.img || `/src/assets/tea/longjing.jpg`" alt="茶叶图片" />
          <div class="order-info">
            <div class="order-title">订单号：<router-link :to="`/orders/${order.id}`">{{ order.id }}</router-link></div>
            <div class="order-status">状态：{{ order.status }}</div>
            <div class="order-price">总价：¥{{ order.totalPrice }}</div>
            <div class="order-tea">茶叶ID：{{ order.teaId }}</div>
            <div class="order-qty">数量：{{ order.quantity }}</div>
          </div>
        </div>
      </div>
      <div v-if="orders.length === 0">暂无订单</div>
      <div class="pagination">
        <button @click="prevPage" :disabled="page === 1">上一页</button>
        <span>第 {{ page }} 页</span>
        <button @click="nextPage">下一页</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import axios from '../api/axios';

const orders = ref<any[]>([]);
const loading = ref(false);
const page = ref(1);
const pageSize = ref(5);
const total = ref(0);

const fetchOrders = async () => {
  loading.value = true;
  try {
    const res = await axios.get(`/order/paged?page=${page.value}&pageSize=${pageSize.value}`);
    orders.value = res.data;
    // 如后端返回 total，可赋值 total.value = res.data.total;
  } catch {
    orders.value = [];
  } finally {
    loading.value = false;
  }
};

const nextPage = () => {
  page.value++;
  fetchOrders();
};
const prevPage = () => {
  if (page.value > 1) {
    page.value--;
    fetchOrders();
  }
};

onMounted(fetchOrders);
</script>

<style scoped>
  .order-list-container {
    max-width: 1200px;
    margin: 40px auto;
    padding: 32px;
    background: #fffbe6;
    border-radius: 16px;
    box-shadow: 0 4px 24px #ffe7ba;
    min-height: 480px;
  }
.order-card-list {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  justify-content: flex-start;
}
.order-card {
  display: flex;
  align-items: center;
  width: 400px;
  background: #fffbe6;
  border-radius: 12px;
  box-shadow: 0 2px 8px #ffd591;
  padding: 16px;
  margin-bottom: 16px;
}
.order-card img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
  margin-right: 16px;
}
.order-info {
  flex: 1;
}
.order-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 4px;
}
.order-status {
  font-size: 14px;
  color: #3b82f6;
  margin-bottom: 4px;
}
.order-price {
  color: #ff6600;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 4px;
}
.order-tea, .order-qty {
  font-size: 14px;
  margin-bottom: 2px;
}
.pagination {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 24px;
}
</style>
